package com.cxz.food.controller;

import com.cxz.food.domain.Food;
import com.cxz.food.domain.FoodClass;
import com.cxz.food.domain.Response;
import com.cxz.food.domain.vo.FoodsResponseVo;
import com.cxz.food.exception.CustomException;
import com.cxz.food.exception.ExceptionEnum;
import com.cxz.food.service.FoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/food")
public class FoodController {

    @Autowired
    private FoodService foodService;


    @RequestMapping(value = "/addFood")
    public String addFood(HttpServletRequest request, ModelMap model) throws Exception{

        HttpSession httpSession = request.getSession();

        Integer businessId = (Integer) httpSession.getAttribute("businessId");

        List<FoodClass> foods = foodService.getFoodClassByBusinessId(businessId);

        model.addAttribute("foodClass",foods);

        model.addAttribute("food",new FoodsResponseVo());

        return "foodAdd";
    }



    @RequestMapping(value = "/add",method = RequestMethod.POST,produces = "application/json")
    public String addFood(HttpSession session ,Food food) throws Exception{

        if(food==null || food.getFoodname() == null){
            throw new CustomException(ExceptionEnum.FOOD_NOT_FIND);
        }

        Integer businessId = (Integer) session.getAttribute("businessId");
        food.setBusinessid(businessId);
        food.setStatus(0);
        if(foodService.addFood(food)==1){
            return "redirect:/food/getFoodList";

        }else {
            return "";
        }
    }


    @RequestMapping(value = "/delete/{foodId}")
    public String deleteFood(@PathVariable Integer foodId) throws Exception{

        if(foodId==null){
            throw new CustomException(ExceptionEnum.FOOD_NOT_FIND);
        }

        foodService.deleteFoodById(foodId);

        return "redirect:/food/getFoodList";

    }

    @RequestMapping(value = "/update/{foodId}")
    public String updateFood(HttpServletRequest request,Model model,@PathVariable Integer foodId) throws Exception{

        FoodsResponseVo food = foodService.findFoodById(foodId);

        Integer businessId = (Integer) request.getSession().getAttribute("businessId");

        List<FoodClass> foods = foodService.getFoodClassByBusinessId(businessId);

        model.addAttribute("foodClass",foods);

        model.addAttribute("food",food);

        return "foodUpdate";
    }



    @RequestMapping(value = "/updateSubmit",method = RequestMethod.POST,produces = "application/json")
    public String updateSubmit(Food food) throws Exception{

        if(food==null){
            throw new CustomException(ExceptionEnum.FOOD_NOT_FIND);
        }
        food.setStatus(0);
        foodService.updateFood(food);
        return "redirect:/food/getFoodList";

    }


//    @RequestMapping("/getFoodClassList")
//    public ModelAndView getFoodClassList(@PathVariable Integer businessId) throws Exception{
//
//
//    }



    @RequestMapping("/getFoodList")
    public String getFoodsByBusinessId(HttpSession session,Model model) throws Exception{

        Integer businessId = (Integer) session.getAttribute("businessId");

        List<Food> foods = foodService.getFoodsByBusinessId(businessId);

        model.addAttribute("foods",foods);

        model.addAttribute("username",session.getAttribute("businessName"));
        return "foodList";
    }



//    @RequestMapping("/getFoodList")
//    public Response getFoodList(@RequestParam int page,@RequestParam int pageSize,@RequestParam int businessId) throws Exception{
//
//        List<Food> foods = foodService.getFoodsByBusinessId(businessId);
//
//        List<FoodClass> foodClass = foodService.getFoodClassByBusinessId(businessId);
//
//        Map<String,Object> map = new HashMap<>();
//        map.put("foodsList",foods);
//        map.put("foodClass",foodClass);
//        return new Response(200,"获取成功",map);
//
//    }


}
